package dbLayer;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import modelLayer.OrderLine;

public class DBOrderLine implements DBOrderLineIF {
	private Connection con;
	
	public DBOrderLine() {
		
		this.con = DBConnection.getInstance().getDBcon();
		
	}

	@Override
	public int insertOrderLine(OrderLine orderLine) throws Exception {
		
		int rc = -1;		
		String query = "INSERT INTO OrderLine (orderID, productID, amount) VALUES ("
					   + "'"
				       + orderLine.getSalesOrder().getOrderID()
				       + "','"
				       + orderLine.getProduct().getProductID()
				       + "','"
				       + orderLine.getAmount()
				       + "')";		
		System.out.println("insert : " + query);		
		try {
			Statement stmt = this.con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		}
		catch (SQLException ex) {
			System.out.println(ex.getMessage());
			throw new Exception ("Order line is not inserted correctly.");
		}		
		return rc;
				
	}

}
